﻿using ArtMath.Flag;
using ArtMath.Resource;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace ArtMath.Window
{
    public partial class AddDocumentWindow : Form
    {
        [Browsable(false)]
        public string DocumentTitle
        {
            get { return tbDocumentTitle.Text; }
        }
        [Browsable(false)]
        public FileStream LinkedFile { get; private set; }
        [Browsable(false)]
        public string Remark
        {
            get { return tbRemark.Text; }
        }
        public AddDocumentWindow()
        {
            InitializeComponent();
            tbDocumentTitle.Text = Resources.Translate("UntitledDocument");
            Flags.Ctrl = false;
        }
        private void ok_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DocumentTitle))
            {
                MessageBox.Show(Resources.JoinLocalizedString("Document", "Title", "CannotBeEmpty"), Resources.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string path = tbSavePath.Text;
            if (!string.IsNullOrEmpty(path))
            {
                if (!path.Contains("\\"))
                {
                    path += Flags.DocumentExtension;
                }
                try
                {
                    LinkedFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Resources.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            DialogResult = DialogResult.OK;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
        private void tbSavePath_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case '\\':
                case '/':
                case ':':
                case '*':
                case '?':
                case '\"':
                case '<':
                case '>':
                case '|':
                    e.Handled = true;
                    ToolTip tt = new ToolTip()
                    {
                        IsBalloon = true,
                        ToolTipTitle = Resources.Translate("Error"),
                        ToolTipIcon = ToolTipIcon.Error,
                    };
                    tt.Show($"{Resources.Translate("FileNameInvalidCharacters")}{Environment.NewLine}\\ / : * ? \" < > |", tbSavePath, 0, -100, 3000);
                    break;
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog()
            {
                Filter = $"{Flags.SoftwareName + Resources.Join + Resources.Translate("Document")}(*{Flags.DocumentExtension})|*{Flags.DocumentExtension}",
                InitialDirectory = Flags.DesktopPath,
            };
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                tbSavePath.Text = sfd.FileName;
            }
        }
    }
}
